package com.misc.tunewsreader;

import java.util.List;

import android.util.Log;

public class NNTPThreadInfo {
	private String subject;
	private String author;
	private String date;
	private String date_sortable;
	private String message_id;
	private int unread;
	private long t_id;

	public NNTPThreadInfo(NNTPArticleHeader header) {
		init(header);
	}

	private void init(NNTPArticleHeader header) {
		int reply_count = NNTPClient.getRepliesCount(header.get("Message-ID"));
		unread = NNTPClient.getRepliesUnreadCount(header.get("Message-ID"));
		if (reply_count == -1) {
			Log.d("NNTP", "Reload " + header.getSubject());
			unread = 0;
			List<NNTPPost> replies = NNTPClient.getReplies(header, 0);
			for (int i = 0; i < replies.size(); i++) {
				if (!replies.get(i).isRead())
					unread++;
			}
			reply_count = replies.size();
			NNTPClient.setRepliesCount(header.get("Message-ID"), reply_count);
			NNTPClient.setRepliesUnreadCount(header.get("Message-ID"), unread);
		}
		message_id = header.get("Message-ID");

		subject = header.getSubject() + " (" + unread + "/" + reply_count + ")";
		if (!header.isRead()) {
			subject = "<b>" + subject + "</b>";
			unread++;
		} else if (unread > 0)
			subject = "<u>" + subject + "</u>";

		author = header.getAuthorName();
		t_id = header.getThreadId();
		date = header.getDate();
		date_sortable = header.getDateSortable();
	}

	public String getSubject() {
		return subject;
	}

	public String getAuthor() {
		return author;
	}

	public long getThreadId() {
		return t_id;
	}

	public String getDate() {
		return date;
	}

	public String getDateSortable() {
		return date_sortable;
	}

	public int getUnread() {
		return unread;
	}

	public void invalidate() {
		NNTPClient.setRepliesCount(message_id, -1);
	}
}
